using System.Linq;
using System.Collections.Generic;
using Managistics.Model;
using NHibernate.Criterion;
using NHibernate;
using System;
using Managistics.Model.ValueObject;
using Managistics.Utility;
using Managistics.Utility.Common;
using Managistics.Core.Services;

namespace Managistics.Core.Repositories.Impl
{
    public class PurchaseOrderRepository : RepositoryBase<PurchaseOrder, string>, IPurchaseOrderRepository
    {

        ///// <summary>
        ///// Select purchase order
        ///// </summary>        
        //public List<PurchaseOrder> GetPurchaseOrder(PurchaseOrderVO param)
        //{
        //    var query = this.Session.QueryOver<PurchaseOrder>();
        //    if (!String.IsNullOrEmpty(param.Id))
        //    {
        //        query.Where(x => x.Orderid.IsLike(param.Id, MatchMode.Anywhere));
        //    }

        //    if (!String.IsNullOrEmpty(param.SupplierName))
        //    {
        //        Supplier supplierReference = null;
        //        query.JoinAlias(x => x.Supplier, () => supplierReference);         
        //        query.Where(x => supplierReference.Name.IsLike(param.SupplierName, MatchMode.Anywhere));
        //    }

        //    if (!String.IsNullOrEmpty(param.GoodCode) || !String.IsNullOrEmpty(param.GoodName))
        //    {
        //        PurchaseOrderDetail purchaseOrderDetail = null;
        //        query.JoinAlias(x => x.PurchaseOrderDetail, () => purchaseOrderDetail);
        //        if (!String.IsNullOrEmpty(param.GoodCode))
        //        {
        //            query.Where(x => purchaseOrderDetail.ItemId.IsLike(param.GoodCode, MatchMode.Anywhere));
        //        }

        //        if (!String.IsNullOrEmpty(param.GoodName))
        //        {
        //            Good good = null;
        //            query.JoinAlias(x => purchaseOrderDetail.Good, () => good);
        //            query.Where(x => good.Name.IsLike(param.GoodName, MatchMode.Anywhere));
        //        }               
        //    }

        //    query.Where(x => x.Orderdate >= DateTimeWithZone.string2DateTime(param.StartDate)
        //            && x.Orderdate <= (DateTimeWithZone.string2DateTime(param.EndDate)));

        //    //pagination
        //    List<PurchaseOrder> result = this.QueryPagination(query, param.page_number, param.page_size);
        //    param.total_row = this.RowCount;
        //    param.total_page = this.TotalPage+1;
        //    return result;                        
        //}

        /// <summary>
        /// Select PurchaseOrders
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<PurchaseOrder> GetPurchaseOrders(PurchaseOrderParam p)
        {
            var query = this.Session.QueryOver<PurchaseOrder>();
            if (!String.IsNullOrEmpty(p.Id))
            {
                query.Where(x => x.Orderid.IsLike(p.Id, MatchMode.Anywhere));
            }

            if (!String.IsNullOrEmpty(p.SupplierName))
            {
                Supplier supplierReference = null;
                query.JoinAlias(x => x.Supplier, () => supplierReference);
                query.Where(x => supplierReference.Name.IsLike(p.SupplierName, MatchMode.Anywhere));
            }

            if (!String.IsNullOrEmpty(p.GoodCode) || !String.IsNullOrEmpty(p.GoodName))
            {
                PurchaseOrderDetail purchaseOrderDetail = null;
                query.JoinAlias(x => x.PurchaseOrderDetail, () => purchaseOrderDetail);
                if (!String.IsNullOrEmpty(p.GoodCode))
                {
                    query.Where(x => purchaseOrderDetail.ItemId.IsLike(p.GoodCode, MatchMode.Anywhere));
                }

                if (!String.IsNullOrEmpty(p.GoodName))
                {
                    Good good = null;
                    query.JoinAlias(x => purchaseOrderDetail.Good, () => good);
                    query.Where(x => good.Name.IsLike(p.GoodName, MatchMode.Anywhere));
                }
            }

            query.Where(x => x.Orderdate >= DateTimeWithZone.string2DateTime(p.StartDate)
                    && x.Orderdate <= (DateTimeWithZone.string2DateTime(p.EndDate)));

            //pagination
            List<PurchaseOrder> result = this.QueryPagination(query, p.page_number, p.page_size);
            p.total_row = this.RowCount;
            p.total_page = this.TotalPage + 1;
            return result;  
        }

        /// <summary>
        /// Get all ordered purchase and detail of it
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="supplierCode"></param>
        /// <param name="goodCode"></param>
        /// <returns></returns>
        public IList<PurchaseOrderDetail> GetPurchaseOrderedDetail(DateTime startDate, DateTime endDate, string supplierCode, string goodCode)
        {
            PurchaseOrder purchaseOrder = null;
            var query = this.Session.QueryOver<PurchaseOrderDetail>()
                .JoinAlias(x => x.PurchaseOrder, () => purchaseOrder)
                .Where(() => purchaseOrder.Orderdate.IsBetween(startDate).And(endDate));

            if (!string.IsNullOrEmpty(supplierCode))
            {
                Supplier supplier = null;
                query.JoinAlias(x => x.PurchaseOrder.Supplier, () => supplier);
                query.Where(() => supplier.Id == supplierCode);
            }

            if (!string.IsNullOrEmpty(goodCode))
            {
                query.Where(x => x.ItemId == goodCode);
            }

            return query.List<PurchaseOrderDetail>();
        }
    }
}